package com.symaster.swingalixcomponent.collapse;

import com.symaster.alixanimation.util.AnimationUtil;
import com.symaster.alixanimation.util.TimeUtil;

import java.awt.*;

/**
 * @author yinmiao
 * @version 2024/2/21
 */
public class CollapseItemLayout implements LayoutManager2 {

    public static final String TOP = "Top";
    public static final String CENTER = "Center";
    private static final Dimension maximumLayoutSize = new Dimension(999, 999);
    private static final Dimension minimumLayoutSize = new Dimension(1, 1);
    private Component top, center;

    @Override
    public void addLayoutComponent(Component comp, Object constraints) {
        if (TOP.equals(constraints)) {
            top = comp;
        } else if (CENTER.equals(constraints)) {
            center = comp;
        }
    }

    @Override
    public Dimension maximumLayoutSize(Container target) {
        return maximumLayoutSize;
    }

    @Override
    public float getLayoutAlignmentX(Container target) {
        return 0;
    }

    @Override
    public float getLayoutAlignmentY(Container target) {
        return 0;
    }

    @Override
    public void invalidateLayout(Container target) {

    }

    @Override
    public void addLayoutComponent(String name, Component comp) {
    }

    @Override
    public void removeLayoutComponent(Component comp) {

    }

    @Override
    public Dimension preferredLayoutSize(Container parent) {
        synchronized (parent.getTreeLock()) {
            int width = 0;
            int height = 0;

            Insets insets = parent.getInsets();
            width += insets.left;
            width += insets.right;
            height += insets.top;
            height += insets.bottom;

            int topWidth = 0;
            if (top != null && top.isVisible()) {
                Dimension preferredSize = top.getPreferredSize();
                height += preferredSize.height;
                topWidth = preferredSize.width;
            }

            int centerWidth = 0;
            if (center != null && center.isVisible()) {
                height += Math.max(center.getHeight(), 0);
                centerWidth = center.getPreferredSize().width;
            }

            width += Math.max(topWidth, centerWidth);

            return new Dimension(width, height);
        }
    }

    @Override
    public Dimension minimumLayoutSize(Container parent) {
        return minimumLayoutSize;
    }

    @Override
    public void layoutContainer(Container parent) {
        synchronized (parent.getTreeLock()) {

            if (parent instanceof CollapseItem) {
                CollapseItem collapseItem = (CollapseItem) parent;

                int centerHeight = center == null ? 0 : center.getPreferredSize().height;

                if (collapseItem.run && center != null) {
                    float v = TimeUtil.floatTime() - collapseItem.runTime;
                    if (v < CollapseItem.execTime) {
                        double point = AnimationUtil.getPoint(CollapseItem.c, v / CollapseItem.execTime);
                        float rotate = (float) point * 90f;

                        int height = collapseItem.openHeight;
                        int round = (int) Math.round(height * point);

                        if (collapseItem.optionType) {
                            centerHeight = round;
                            collapseItem.rotateIcon.setAngle(rotate);
                        } else {
                            centerHeight = height - round;
                            collapseItem.rotateIcon.setAngle(90f - rotate);
                        }
                    } else {
                        if (collapseItem.optionType) {
                            centerHeight = collapseItem.openHeight;
                            collapseItem.rotateIcon.setAngle(90);
                        } else {
                            centerHeight = 0;
                            collapseItem.rotateIcon.setAngle(0);
                        }

                        collapseItem.run = false;
                        center.setVisible(collapseItem.optionType);
                        collapseItem.openListeners.forEach(e2 -> e2.after(collapseItem.optionType));
                    }
                }

                Insets insets = parent.getInsets();
                int left = insets.left;
                int right = insets.right;
                int top = insets.top;
                int bottom = insets.bottom;

                int width = parent.getWidth();
                int itemWidth = width - left - right;

                if (this.top != null) {
                    Dimension preferredSize = this.top.getPreferredSize();
                    this.top.setBounds(left, top, itemWidth, preferredSize.height);
                    top += preferredSize.height;
                }

                if (center != null) {
                    center.setBounds(left, top, itemWidth, centerHeight);
                }
            }
        }
    }
}
